
//
//  ByViewMyHeader.m
//  jinpuyiyuan_ios
//
//  Created by mc on 2017/12/6.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByViewMyHeader.h"

// vc
#import "ByUserMyDotorsRecordVC.h"              // 就诊记录
#import "ByUserMyPayRecordVC.h"                 // 缴费记录
#import "ByUserMyMedicalRecordVC.h"             // 病史记录
#import "ByUserMyDataVC.h"                      // 个人资料
@implementation ByViewMyHeader
{
    UIImageView         *_bgImg;
    UIImageView         *_icon;
    UIImageView         *_gradeImg;
    UILabel             *_nickNameLabel;
    
    UIView              *_bgFuctionView;
}
- (instancetype)init{
    if (self = [super init]) {
        [self createViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)createViews{
    _bgImg = [[UIImageView alloc] init];
    [self addSubview:_bgImg];
    [_bgImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 250));
    }];
    _bgImg.image = [UIImage imageNamed:@"My_beijing"];
    
    // 头像
    _icon = [[UIImageView alloc] init];
    [self addSubview:_icon];
    [_icon mas_makeConstraints:^(MASConstraintMaker *make) {
        if (SCREEN_HEIGHT == 812) {
            make.top.equalTo(@(88 + 10));
        }else{
            make.top.equalTo(@(64 + 15));
        }
        make.centerX.equalTo(self.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    _icon.layer.cornerRadius = 70 / 2;
    UITapGestureRecognizer *iconTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap)];
    _icon.userInteractionEnabled = YES;
    [_icon addGestureRecognizer:iconTap];
    _icon.image = DEFAULT_IMAGE;
    _icon.layer.masksToBounds = YES;
    
    // 昵称
    CGFloat width = [@"用户名" getSizeOfString:14].width + 15;
    _nickNameLabel = [[UILabel alloc] init];
    [self addSubview:_nickNameLabel];
    [_nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@((SCREEN_WIDTH - 30 - width) / 2));
        make.top.equalTo(_icon.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    _nickNameLabel.font = FONT(14);
    _nickNameLabel.textColor = [UIColor whiteColor];
    UITapGestureRecognizer *nameTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickTap)];
    _nickNameLabel.userInteractionEnabled = YES;
    [_nickNameLabel addGestureRecognizer:nameTap];
    
    // 级别
    _gradeImg = [[UIImageView alloc] init];
    [self addSubview:_gradeImg];
    [_gradeImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nickNameLabel.mas_right);
        make.top.equalTo(_nickNameLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    _gradeImg.image = [UIImage imageNamed:@"My_renzheng"];
    _gradeImg.hidden = YES;
    
    // 功能模块视图
    _bgFuctionView = [[UIView alloc] init];
    [self addSubview:_bgFuctionView];
    [_bgFuctionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(10));
        make.top.equalTo(_bgImg.mas_bottom).offset(-45);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 20, 90));
    }];
    _bgFuctionView.layer.cornerRadius = 4.0;
    _bgFuctionView.layer.borderColor = [UIColor colorWithWhite:0.90 alpha:1].CGColor;
    _bgFuctionView.layer.borderWidth = 1;
    _bgFuctionView.backgroundColor = [UIColor whiteColor];
    [self createFuctionViews];
    
}


- (void)setModel:(ByModelUser *)model{
    _model = model;
    
    // 测试数据
    _model.username = @"测试测试昵称";
    _nickNameLabel.text = _model.username;
    
    // 昵称
    CGFloat width = [_model.username getSizeOfString:14].width + 15;
    [_nickNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@((SCREEN_WIDTH - 30 - width) / 2));
        make.top.equalTo(_icon.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(width, 20));
    }];
    
    // 级别
    [_gradeImg mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nickNameLabel.mas_right);
        make.top.equalTo(_nickNameLabel.mas_top);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    _gradeImg.hidden = NO;
    
}

// 创建功能模块
- (void)createFuctionViews{
    for (UIView *subview in _bgFuctionView.subviews) {
        [subview removeFromSuperview];
    }
    NSArray *titles = @[@"就诊记录",@"缴费记录",@"病史记录"];
    NSArray *imgs = @[@"My_jiuzhenjilu",@"My_jiaofeijilu",@"My_bingshijilu"];
    CGFloat width = [titles[0] getSizeOfString:14].width + 10;
    CGFloat margin = 20;
    for (int i = 0;i < titles.count;i ++ ) {
        CGFloat x = margin + (SCREEN_WIDTH - 20 - 20 * 2 - width * titles.count) / 2 * i + width * i;
        UIControl *bgControl = [[UIControl alloc] init];
        [_bgFuctionView addSubview:bgControl];
        bgControl.tag = 10 + i;
        [bgControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(x));
            make.top.equalTo(@(15));
            make.size.mas_equalTo(CGSizeMake(width, 30 + 10 + 20));
        }];
        
        UIImageView *img = [[UIImageView alloc] init];
        [bgControl addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@((width - 30) / 2));
            make.top.equalTo(@(0));
            make.size.mas_equalTo(CGSizeMake(30, 30));
        }];
        img.image = [UIImage imageNamed:imgs[i]];
        
        UILabel *label = [[UILabel alloc] init];
        [bgControl addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(0));
            make.top.equalTo(img.mas_bottom).offset(10);
            make.size.mas_equalTo(CGSizeMake(width, 20));
        }];
        label.textColor = [UIColor blackColor];
        label.font = FONT(14);
        label.text = titles[i];
        [bgControl addTarget:self action:@selector(controlAction:) forControlEvents:UIControlEventTouchUpInside];
     
    }
}


// 点击事件
- (void)controlAction:(UIControl *)control{
//    ByModelUser *user = [ByCacheUser getUserInfo];
//    if (!user) {
//        ByLoginVC *vc = [[ByLoginVC alloc] init];
//        vc.typeFrom = @"1";
//        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
//        [self.viewController presentViewController:navi animated:YES completion:nil];
//        return;
//    }
    if (![self isLogin]) {
        return;
    }
    // 就诊记录
    if (control.tag == 10) {
        ByUserMyDotorsRecordVC *vc = [[ByUserMyDotorsRecordVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    // 缴费记录
    else if (control.tag == 11){
        ByUserMyPayRecordVC *vc = [[ByUserMyPayRecordVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    // 病史记录
    else{
        ByUserMyMedicalRecordVC *vc = [[ByUserMyMedicalRecordVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];

    }
    
}

//
- (void)clickTap{
    if (![self isLogin]) {
        return;
    }
    ByUserMyDataVC *vc = [[ByUserMyDataVC alloc] init];
    [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
}

#pragma mark - 判断是否登录
- (BOOL)isLogin{
    ByModelUser *user = [ByCacheUser getUserInfo];
    if (!user) {
        ByLoginVC *vc = [[ByLoginVC alloc] init];
        vc.typeFrom = @"1";
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self.viewController presentViewController:navi animated:YES completion:nil];
        return NO;
    }
    return YES;
}
@end
